from google.appengine.ext import bulkload
from google.appengine.api import datastore_types
from google.appengine.ext import search
from pyth.inventionmodels import *
import datetime, os
import logging

def photoFilter(ph):
    if ph:
      return "mainimages/" + ph.split('/')[1] + ".jpg"
    else:
      return "mainimages/01_11a.jpg"

#def getPhotos(photoFolder):
#    photofolder = photoFolder.split('/')[1]
#    photos = []
#    albumdir = '/Users/ollie/Desktop/CraftComputing/albumimages/'
#    files = os.listdir(albumdir + photofolder)
#    for f in files:
#        base, ext = os.path.splitext(f)
#        if ext in ['.png', '.jpg', '.gif']:
#            photos.append(photoFolder + f)
#    return ', '.join(photos)
#    try:
#        photofolder = photoFolder.split('/')[1]
#        photos = []
#        albumdir = '/Users/ollie/Desktop/CraftComputing/albumimages/'
#        files = os.listdir(albumdir + photofolder)
#        for f in files:
#            print f
#            base, ext = os.path.splitext(f)
#            if ext in ['.png', '.jpg', '.gif']:
#                photos.append(photoFolder + f)
#        return photos
#    except Exception, e:
#        print e
#        return ['']


class WorkshopLoader(bulkload.Loader):
  def __init__(self):
    # Our 'Person' entity contains a name string and an email
    #print 'here'
    bulkload.Loader.__init__(self, 'Workshop', 
                         [('codeId', lambda x: x.replace('\x0b', ' ').replace('\x1d', ' ').split(' ')),
                            ('insp', lambda x: datastore_types.Text(x.replace('\x0b', ' ').replace('\x1d', ' '))),
                            ('facilitators' , lambda x: datastore_types.Text(x.replace('\x0b', ' ').replace('\x1d', ' '))),
                            ('longDesc' , lambda x: datastore_types.Text(x.replace('\x0b', ' ').replace('\x1d', ' '))),
                            ('craft' , lambda x: x.replace('\x0b', ' ').replace('\x1d', ' ').split(',')),
                            ('hardware' ,lambda x: x.replace('\x0b', ' ').replace('\x1d', ' ').split(',')),
                            ('software' ,lambda x: x.replace('\x0b', ' ').replace('\x1d', ' ').split(',')),
                            ('recordId' , str),
                            ('date' , str),
                            ('shortDesc' , lambda x: datastore_types.Text(x.replace('\x0b', ' ').replace('\x1d', ' '))),
                            ('title' , str),
                            ('photoFolder' , photoFilter),
                            ('location' , str),
                            ('photos', lambda x: x.split(', ')) 
                          ])

  def HandleEntity(self, entity):
#    w = Workshop()  
#    w.codeId = entity['codeId']
#    w.insp = entity['insp']
#    w.age1 = entity['age1']
#    w.age2 = entity['age2']
#    w.ageAvg = entity['ageAvg']
#    w.cost = entity['cost']
#    w.facilitators = entity['facilitators']
#    w.longDesc = entity['longDesc']
#    w.time = entity['time']
#    w.craft = entity['craft']
#    w.hardware = entity['hardware']
#    w.software = entity['software']
#    w.recordId = entity['recordId']
#    w.date = entity['date']
#    w.shortDesc = entity['shortDesc']
#    w.title = entity['title']
#    w.photoFolder = entity['photoFolder']
#    w.photos = entity['photos']      
#    exists = Workshop.gql("WHERE recordId = :1", entity['recordId'])
#    if exists.count() == 0 or (exists.count() == 1 and exists[0].datetime < datetime.datetime.now()):
#    w.put()
    return search.SearchableEntity(entity)

if __name__ == '__main__':
  bulkload.main(WorkshopLoader())